package com.itheima.wemedia.controller;


import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.dto.DownOrUpDto;
import com.itheima.wemedia.pojo.WmNews;
import com.itheima.wemedia.dto.WmNewsDtoSave;
import com.itheima.wemedia.dto.WmNewsPageReqDto;
import com.itheima.wemedia.service.WmNewsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import com.itheima.core.controller.AbstractCoreController;

import java.util.Map;

/**
 * @version 1.0
 * @description <p>自媒体图文内容信息</p>
 * @package com.itheima.wemedia.controller
 */
@Api(value = "WmNewsController", tags = "自媒体图文内容信息")
@RestController
@RequestMapping("/news")
public class WmNewsController extends AbstractCoreController<WmNews> {

    private WmNewsService wmNewsService;

    @Autowired
    public WmNewsController(WmNewsService wmNewsService) {
        super(wmNewsService);
        this.wmNewsService = wmNewsService;
    }


    /**
     * 文章分页查询
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public PageResultVo<WmNews> findPage(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.pageResult(dto);
    }


    /**
     * 发布文章
     * @param draft
     * @param dto
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("发布文章")
    public ResultVo<Integer> submit(Boolean draft, @RequestBody WmNewsDtoSave dto) {
        wmNewsService.submit(draft, dto);
        return ResultVo.ok();
    }


    /**
     * 文章id查询
     * @param newId
     * @return
     */
    @GetMapping("/one/{newId}")
    @ApiOperation("文章id查询")
    public ResultVo<WmNews> findOneById(@PathVariable (value = "newId") Integer newId){
        WmNews wmNewsServiceById = wmNewsService.getById(newId);
        return ResultVo.ok(wmNewsServiceById);
    }


    /**
     * 通过id删除
     * @param newsId
     * @return
     */
    @ApiOperation("文章删除")
    @GetMapping("/del_news/{newsId}")
    public ResultVo deleteById(@PathVariable(value = "newsId") int newsId){
        wmNewsService.deleteById(newsId);
        return ResultVo.ok();
    }

    /**
     * 文章 上|下架
     * @param dto
     * @return
     */
    @ApiOperation("文章上架或下架")
    @PostMapping("/down_or_up")
    public ResultVo downOrUp(@RequestBody DownOrUpDto dto){
        wmNewsService.downOrUp(dto);
        return ResultVo.ok();
    }
}

